package com.lianboke.link.ad.updateUtils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * app 的工具类
 * Created by admin on 2017/10/13.
 */

public class AppUtils {

    // 判断本地是否有相应的应用
    public static boolean hasApp(Context context, String packageName) {
        boolean hasapp = false;
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            hasapp = true;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return hasapp;
    }

    // 判断软件是否升级
    public static int isVersionUpdate(String newVersionName,PackageInfo oldPackageInfo) {
        int isUpdate = 0;
        try {
            String oldVersionName = oldPackageInfo.versionName;
            if (getVersionName(oldVersionName) < getVersionName(newVersionName)) {
                isUpdate = 1;//升级
            } else {
                isUpdate = 2;//打开
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isUpdate;
    }
    // 把版本转成int类型
    public static long getVersionName(String versionName) {
        long num = 0;
        if (versionName != null || "".equals(versionName)) {
            num = Long.parseLong(versionName.replace(".", ""));
        }
        return num;
    }
    // 通过本地apk路径，获取包名和版本信息（返回info）
    public static PackageInfo getPackageInfoByLoad(String path,Context context)
    {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
        return info;
    }

    /**
     * 根据package 获取指定packageInfo 信息
     * @param packname
     * @param context
     * @return
     */
    public static PackageInfo getPackageInfo(String packname,Context context)
    {
        PackageInfo info =null;
        try {
            info = context.getPackageManager().getPackageInfo(packname, 0);
            return info;
        }catch (Exception e){

        }
        return info;
    }



}
